layui.use(['jquery', 'layer'], function() {

    var $ = layui.jquery;
    var layer = layui.layer;
    let token = localStorage.getItem('token'); //获取用户token

    //查询文章分类
    var querylist = function() {
        $.ajax({
            method: 'GET',
            url: '/my/article/cates',
            success: function(result) {
                if (result.status == 0) {
                    console.log(result);

                    let trs = '';
                    result.data.forEach(function(item) {
                        let tr =
                            `<tr><td id='booksid' name='Id' style='display:none'>${item.Id}</td><td>${item.name}</td><td>${item.alias}</td>
                                <td><button type="button" class="layui-btn layui-btn-normal editbooks">编辑</button><button type="button" class="layui-btn layui-btn-danger delbooks">删除</button></td></tr>`;

                        trs += tr;


                    })
                    $('#books').append(trs);
                }
            }
        })
    }
    querylist();


    //添加分类弹出层
    $('#additem').click(function() {

        layer.open({
            type: 2,
            content: 'addclass.html', //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            area: ['500px', '300px'],

        });

    })
    //添加编辑弹出层
    $(document).find('#books').on('click', '.editbooks', function() {
        let id = $(this).parents('tr').find('#booksid').html();

        layer.open({
            type: 2,
            content: 'editclass.html?id=' + id, 
            area: ['500px', '300px'],

        });

    })

    //删除文章分类
    $(document).find('#books').on('click', '.delbooks', function() {
        var tr = $(this).parents('tr');
        var id = tr.find('td:first').html();
        console.log(id);
        layer.confirm('是否删除', {
            icon: 3,
            title: '提示'
        }, function(index) {
            //do something
            $.ajax({
                method: 'GET',
                url: '/my/article/deletecate/' + id,
                success: function(result) {
                    console.log(result);
                    layer.msg('删除成功！', {
                        time: 1000
                    }, function() {

                        tr.remove();
                    });
                }

            })

            layer.close(index);
        });


    })



});
